﻿using Elasticsearch.Net;
using Nest;
using System;
using System.Collections.Generic;
using System.Text;
using ZYHand.Entity;
using ZYHand.Utility;

namespace ZYHand.Repository
{
    public class ElasticSearchClient
    {
        private static ElasticSearchSetting elasticSearchSetting;//连接配置

        private static ElasticSearchClient elasticSearchClient = null;
        // 默认索引
        //public static string DEFAULT_INDEX = "resource";

        public ElasticSearchClient(string DEFAULT_INDEX) 
        {
            elasticSearchSetting = new ElasticSearchSetting();
            elasticSearchSetting.Urls = AppSettingHelper.GetConfiguration().GetSection("Elasticsearch:Urls").Value;
            elasticSearchSetting.DEFAULT_INDEX = DEFAULT_INDEX?? AppSettingHelper.GetConfiguration().GetSection("Elasticsearch:DEFAULT_INDEX").Value;
        }
        public ElasticClient Client()
        {
            var nodes = new Uri[]
            {
              new Uri(elasticSearchSetting.Urls)
            };
            var pool = new StaticConnectionPool(nodes);
            var settings = new ConnectionSettings(pool)
                .DefaultIndex(elasticSearchSetting.DEFAULT_INDEX)
                .PrettyJson();
            //.BasicAuthentication("elastic", "changeme");
            
            return new ElasticClient(settings);
        }

        public static ElasticSearchClient GetInstance(string DEFAULT_INDEX)
        {
            if (elasticSearchClient == null)
            {
                elasticSearchClient = new ElasticSearchClient(DEFAULT_INDEX);
            }
            return elasticSearchClient;
        }
    }
}
